//
//  CWCFunnyViewController.m
//  ChuanNews
//
//  Created by dllo on 16/4/21.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CWCFunnyViewController.h"
#import "BaseHeader.h"
@interface CWCFunnyViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, retain)MBProgressHUD *hud;
@property(nonatomic, assign)NSInteger page;
@property(nonatomic, assign)BOOL isUpLoading;
@end

@implementation CWCFunnyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.array = [NSMutableArray array];
    
    //加载菊花
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.hud.labelText = @"loading......";
    self.hud.detailsLabelText = @"网络正在加载,请稍后......";
    self.hud.color = [UIColor blackColor];
    self.hud.dimBackground = YES;
    self.page = 10;
    [self creatDataWith:self.page];
    [self creatView];
    self.isUpLoading = NO;
    [self addFooter];
    [self addHeader];
}
- (void)addHeader{
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.isUpLoading = NO;
        self.page = 10;
        [self creatDataWith:self.page];
    }];
}

- (void)addFooter {
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        self.isUpLoading = YES;
        self.page += 10;
        [self creatDataWith:self.page];
    }];
}
- (void)creatDataWith:(NSInteger)page{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:[NSString stringWithFormat:@"http://c.3g.163.com/recommend/getChanRecomNews?channel=duanzi&passport=&devId=54555A0E-9F1B-4481-AEF4-F84D43BD8764&size=%ld",self.page] parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (self.isUpLoading == NO) {
            //[self.array removeAllObjects];
        }
        NSArray *Array = [responseObject objectForKey:@"段子"];
        for (NSDictionary *dic in Array) {
            CWCFunModel *fun = [[CWCFunModel alloc]initWithDic:dic];
            [self.array addObject:fun];
            [fun release];
        }
                dispatch_async(dispatch_get_main_queue(), ^{
            [self.hud hide:YES afterDelay:0];
            [self.tableView.mj_footer endRefreshing];
            [self.tableView.mj_header endRefreshing];
        });
        [self.tableView reloadData];
     
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    }];
}


-(void)creatView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView release];
    [self.tableView registerClass:[CWCFunnyCell class] forCellReuseIdentifier:NSStringFromClass([CWCFunnyCell class])];
    [self.tableView registerClass:[CWCFunnyImageCell class] forCellReuseIdentifier:NSStringFromClass([CWCFunnyImageCell class])];

}
    
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.array.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CWCFunModel *model = self.array[indexPath.row];
    if (model.imgsrc == nil) {
        CWCFunnyCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([CWCFunnyCell class]) forIndexPath:indexPath];
        cell.model = model;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else{
        CWCFunnyImageCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([CWCFunnyImageCell class]) forIndexPath:indexPath];
        cell.model = model;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CWCFunModel *model = self.array[indexPath.row];
    if (model.imgsrc == nil) {
        CGFloat height = [CWCFunnyCell textLabelHeight:model.digest];
        return height + 40.0;
    }else{
        CGFloat height = [CWCFunnyCell textLabelHeight:model.digest];
        //pixel是图片的尺寸
        NSString *str = model.pixel;
        NSRange range = NSMakeRange(0, 3);
        NSRange range1 = NSMakeRange(4, 3);
        NSString * widthString=[str substringWithRange:range];
        CGFloat imgWidth=[widthString floatValue];
        NSString * heightString=[str substringWithRange:range1];
        CGFloat imgHeight =[heightString floatValue];
        CGFloat Height = 355 * imgHeight / imgWidth;
        return height + Height + 20;
    }
}

-(void)dealloc{
    [_hud release];
    [_tableView release];
    [super dealloc];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
